package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author gorkr
 * @date 2022/08/29 23:39
 **/
@Controller
public class IndexController {

    @Resource
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redisson;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        // model 中 属性会放到请求域中
        //1、查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categories",categoryEntities);
        // 试图解析器进行拼串
        // classpath:/templates/ + 返回值 + .html

        return "index";

    }

    /**
     * 二级分类
     * @return
     */
    //index/json/catalog.json
    @GetMapping(value = "/index/json/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        System.out.println("/index/catalog.json");


        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();

        return catalogJson;

    }

    @GetMapping(value = "/hello_redisson")
    public String hello(){
        //1、获取一把锁，只要名字一样 就是同一把锁
        RLock lock = redisson.getLock("my-lock");
        //2、加锁
        lock.lock();  // 阻塞式等待 默认加锁时间30s
        //1、锁自动续期 自动续30s 不用担心业务执行时锁过期
        //2、加锁业务只要运行完成 就不再续期 业务突然终端 也会因为过期释放锁
        try {
            System.out.println("加锁完成，执行业务..."+ Thread.currentThread().getId());
            Thread.sleep(15000);
        } catch (InterruptedException e) {

        } finally {

            // 解锁  解锁因为中断没有执行  也会因为过期 释放锁

            lock.unlock();
            System.out.println("业务完成，释放锁..."+ Thread.currentThread().getId());
        }
        return "结束";
    }

//    @GetMapping(value = "/read_writer_lock")

    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.writeLock();
        try {
            // 改数据 加写锁
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue",s);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        // 加读锁
        RLock rLock = lock.readLock();
        try {
            rLock.lock();
            s = redisTemplate.opsForValue().get("writeValue");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    /**
     * 车库停车
     * 3车位
     */
    // 信号量 可以应用于限流
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.acquire();


        return "ok";
    }

    public String go() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();


        return "ok";
    }

    /**
     * 房价锁门
     * 1班没人了 2班没人了
     * 5个班全部走完  才能锁大门
     */
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();

        return "放假了。。。";
    }
    public String gogo(@PathVariable("id") Long id){
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();
        return id+"班的人都走了";
    }

}
    